package com.xja.zufang.controller;

import com.xja.zufang.entity.Users;
import com.xja.zufang.service.Userservice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("user")
@SessionAttributes("loginusers")
public class controller {
    @Autowired
    private Userservice userservice;
    @RequestMapping("login")
    public String login(Model model, Users users){
        try {
            Users loginusers = userservice.selectLogin(users.getName());
            System.out.println(users.getName());
            System.out.println(users.getPassword());
          if (loginusers==null){
              model.addAttribute("msg","用户名错误");
              return "redirect:/login.jsp";
          }
        if (!loginusers.getPassword().equals(users.getPassword())){
            model.addAttribute("msg","密码错误");
            return "redirect:/login.jsp";
        }

        //登录成功
            model.addAttribute("loginusers",loginusers);
             return "redirect:/list.jsp";

        }catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }

    @RequestMapping("reg")
    public String addreg(Users user){
        userservice.addreg(user);
        return "redirect:/login.jsp";
    }
}
